const app = getApp();
const utils = require('../../utils/utils.js');
Page({
    data: {
        used: {},
        isClick: true,
        payClick: true,
        checkNum: 0,
        used_id: 0,
        order_id: 0,
        timer: '',
    },
    onLoad: function (options) {
        let usedId = options.used_id;
        this.getUsedInfo(usedId);
    },
    onHide: function () {
        this.setData({
            checkNum: 0,
            isClick: true,
            payClick: true
        });
        clearInterval(this.data.timer);
    },
    onUnload: function () {
        this.setData({
            checkNum: 0,
            isClick: true,
            payClick: true
        });
        clearInterval(this.data.timer);
    },
    // 复制订单号
    handleCopy: function (e) {
        let usedsn = e.currentTarget.dataset.usedsn;
        wx.setClipboardData({
            data: usedsn,
            success(res) {
                wx.showToast({
                    title: "复制成功",
                    icon: 'none',
                    duration: 2000
                });
            }
        });
    },
    // ============================ 获取使用记录信息 ========================
    getUsedInfo(usedId) {
        let that = this;
        // 获取该使用记录的详细信息
        wx.showLoading({
            title: 'loading...',
            mask: true
        });
        utils.httpRequest('/getUsedInfo', {
            used_id: usedId
        }, function (res) {
            if (res.code == 1) {
                that.setData({
                    used: res.data,
                    sn: res.data.rentDeviceSn
                })
            }
            wx.hideLoading();
        }, function () {
        })

    },
    // ======================= 未支付订单调起支付 ===========================
    toPay(e) {
        let usedId = e.currentTarget.dataset.id;
        this.setData({
            used_id: usedId
        });
        this.goToPay();
    },

    // ======================= 发起扣款 =================
    goToPay() {
        wx.showLoading({
            title: '请稍后...',
            mask: true
        });
        let that = this;
        let used_id = that.data.used_id;
        let order_id = that.data.order_id;
        if (that.data.payClick) {
            that.setData({
                payClick: false
            });
            // 第一步先校验是否是需要支付的微信支付分订单
            utils.httpRequest('/doToPay', {
                used_id: used_id,
                order_id: order_id
            }, function (res) {
                switch (res.code) {
                    case 1:
                        wx.hideLoading();
                        wx.showToast({
                            title: res.msg,
                            icon: 'none',
                            duration: 2000
                        });
                        // 支付成功，重新加载数据
                        that.getUsedInfo(res.data.used_id);
                        break;
                    case 2:
                        // 未支付。需要发起支付
                        utils.wxPayPayment(res.data.amount, res.data.type, res.data.sn, res.data.used_id, function (order_id) {
                            wx.hideLoading();
                            // 查询用户是否支付
                            if (order_id != null) {
                                that.checkOrderStatus(order_id);
                            } else {
                                that.setData({
                                    payClick: true
                                });
                            }
                        }, function () {
                            that.setData({
                                payClick: true
                            });
                            wx.hideLoading();
                        });
                        break;
                    default:
                        that.setData({
                            payClick: true
                        });
                        wx.hideLoading();
                        break;
                }
            }, function () {
                that.setData({
                    payClick: true
                })
            })
        }
    },

    // ====================== 校验用户是否支付成功  ===========================================
    checkOrderStatus(order_id) {
        let that = this;
        let check = that.data.checkNum;
        check++;
        that.setData({
            checkNum: check
        })
        if (that.data.checkNum < 10) {
            utils.httpRequest('/checkOrderStatus', {
                order_id: order_id,
                freeDeposit: 0
            }, function (res) {
                if (res.data.status == 1) {
                    wx.hideLoading();
                    that.setData({
                        order_id: res.data.order_id,
                        payClick: true
                    });
                    clearInterval(that.data.timer);
                    that.goToPay();
                } else {
                    var timerTem = setTimeout(() => {
                        that.checkOrderStatus(order_id);
                    }, 1500);
                    that.setData({
                        timer: timerTem
                    });
                }
            }, function () {
                wx.hideLoading();
            })
        } else {
            wx.showToast({
                title: "订单异常,请联系客服",
                icon: 'none',
                duration: 2000
            });
            wx.hideLoading();
        }
    },

});